# -*- coding: utf-8 -*-
'''
@Software: PyCharm
@File    : comments_emotion.py
@Author  : Bryan SHEN
@E-mail  : m18801919240_3@163.com
@Site    : Shanghai, China
@Time    : 2021-11-01
@Description: 
'''

from LiveBarrageSentiment.func.barrage_text_match import BarrageTextMatch
import json

import pandas as pd

textmatch = BarrageTextMatch()

dy_data = pd.read_excel("dy_comments.xlsx")

items = dy_data.to_dict(orient='records')

print("-----items total : {}-----".format(len(items)))


for i, item in enumerate(items):

    if i % 50000 == 0: print(i)
    res = json.loads(textmatch.run([str(item["comments"])]))
    feedback = res["feedback"]
    item["emotion"] = feedback[0]["emotion"]
    item["subject"] = feedback[0]["subject"]
    item["point"] = feedback[0]["point"]


columns = ["match_keyword", "post_id", "comments", "emotion", "subject", "point"]

new_data = pd.DataFrame(items)

output_path = "dy_comments_emotion_20211101.xlsx"
with pd.ExcelWriter(output_path, engine='xlsxwriter', options={'strings_to_urls': False}) as writer:
    new_data.to_excel(writer, sheet_name='part1', index=False, columns=columns)
